/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spoxcy.business;

import com.spoxcy.po.UserProfile;
import com.spoxcy.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Denis.Duan
 * @date Apr 16, 2009
 */
public class RegistrationServiceImpl implements IRegistrationService {

    DBUtil dbUtility;
    private final String INSERT_SQL = "insert into t_user_info (first_name,surname," +
            "user_name, contact_num,pword,address ) values (?,?,?,?,?,?)";

    //constructor that requires DBUtil object
    public RegistrationServiceImpl(DBUtil dbUtility) {
        this.dbUtility = dbUtility;
    }

    //insert user profile into database
    public boolean addProfile(UserProfile profile) {

        boolean flag = false;
        Connection connection = null;

        try {
            connection = this.dbUtility.getConnection();
            PreparedStatement statement = connection.prepareStatement(INSERT_SQL);

            statement.setString(1, profile.getFirstName());
            statement.setString(2, profile.getSurname());
            statement.setString(3, profile.getUserName());
            statement.setString(4, profile.getContactNum());
            statement.setString(5, profile.getPassword());
            statement.setString(6, profile.getAddress());

            flag = statement.execute();
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return flag;
    }


    //for testing purpose
    public static void main(String[] args) throws SQLException {

        UserProfile profile = new UserProfile();
        profile.setFirstName("Denis");
        profile.setSurname("Duan");
        profile.setUserName("little Denis");
        profile.setPassword("happy");
        profile.setContactNum("66617883618");
        profile.setAddress("manson st, Maroubra");

        //RegistrationServiceImpl registrator = new RegistrationServiceImpl();
        //registrator.addProfile(profile);
    }
}
